﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Lari.Configuration;
using Lari.Services.Data;
using Lari.Services.Implementation;
using Lari.Services.ViewModel;
using Lari.Web.Utility;

namespace Lari.Web.Controllers
{
    public class HrMasterController : LariController
    {
        private readonly MasterManager _mgr;

        public HrMasterController(MasterManager mgr)
        {
            _mgr = mgr;
        }
        [AccessFilter(AccessConst.Master.Hr.ManageQualification)]
        public ActionResult Qualifications(int currentPage = 1)
        {
            var model = new ListModel<QualificationModel> { CurrentPage = currentPage };
            try
            {
                int totalPageCount = 0;
                var result = new List<QualificationModel>();
                List<Qualification> record = _mgr.Qualifications(currentPage, ref totalPageCount);
                if (record != null)
                {
                    record.ForEach(r => result.Add(r.ConvertToViewModel()));
                    model.Result = result;
                    model.TotalPageCount = totalPageCount;

                }
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.QalificationFetchAllFailed, ex, "HrMasterController Qualifications");
            }

            return View(model);
        }

        [AccessFilter(AccessConst.Master.Hr.CreateQualification)]
        public ActionResult CreateQualification()
        {
            return View(new QualificationModel());
        }
        [HttpPost]
        [AccessFilter(AccessConst.Master.Hr.CreateQualification)]
        public ActionResult CreateQualification(QualificationModel model)
        {

            if (!ModelState.IsValid)
            {
                return View(model);
            }
            try
            {
                _mgr.Save(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Hr.Qualifications, ControllerConst.Masters.HRMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.QualificationSavedFailed, ex, "HrMasterController CreateQualification post");
            }
            return View(model);

        }


        [AccessFilter(AccessConst.Master.Hr.ModifyQualification)]
        public ActionResult ModifyQualification(int id)
        {
            var model = new QualificationModel();
            try
            {
                var data = _mgr.Qualification(id);
                if (data == null)
                    return NoDataFound;

                model = data.ConvertToViewModel();
                if (data.Parent.HasValue && data.Parent.Value > 0)
                {
                    model.NotDegree = true;
                }
                else
                {
                    model.NotDegree = false;
                }

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.QualificationFetchFailed, ex, "HrMasterController ModifyQualification");
            }
            return View(model);
        }
        [HttpPost]
        [AccessFilter(AccessConst.Master.Hr.ModifyQualification)]
        public ActionResult ModifyQualification(QualificationModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            try
            {
                _mgr.Update(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Hr.Qualifications, ControllerConst.Masters.HRMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.QualificationUpdateFailed, ex, "HrMasterController ModifyQualification post");
            }
            return View(model);
        }
    }
}